package com.safe.ele.service.impl;




import com.safe.ele.config.RedisKeyConfig;
import com.safe.ele.dao.IProductDao;
import com.safe.ele.dto.ProdectDto;
import com.safe.ele.entity.IProduct;
import com.safe.ele.service.intf.IProductService;
import com.safe.ele.util.RedissonUtil;
import com.safe.ele.vo.R;
import org.redisson.Redisson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
/**
 * @program: springboot
 * @description: 十一组后端源码
 * @author: Feri(十一组)
 * @create: 2021-01-09 17:39:00
 */
@Service
public class IProductServiceImpl implements IProductService {
    /*
    *注入持久层对象*/
    @Autowired
    private IProductDao dao;
    /*
    * 新增内容*/
    @Override
    public R save(IProduct iProduct) {
        if(iProduct!=null){
            if(dao.insert(iProduct)>0){
                return R.ok();
            }
        }
        return R.fail("新增异常");
    }
    /*
    * 查询 所有数据*/
    @Override
    @Scheduled(cron = "0 0 0 1/1 * ?") //每天0：00 执行一次
    public void queryAll() {
        List<ProdectDto> prodectDtos = dao.selectAll();
        //将主页数据放入redis
        for(ProdectDto p: prodectDtos){
            RedissonUtil.setSet(RedisKeyConfig.INDEX_KEY,p);
        }
    }

    /*
     * 删除 根据ID*/
    @Override
        public R DelAll(IProduct iProduct) {
           if(iProduct!=null){
                       if(dao.DelAll(iProduct)>0){
                           return R.ok();
                       }
                   }
                   return R.fail("删除异常");
        }

      /*
      * 根据ID跟新数据*/
     @Override
         public R updateById(IProduct iProduct) {
            if(iProduct!=null){
                   if(dao.updateById(iProduct)>0){
                         return R.ok();
                      }
                 }
                  return R.fail("跟新异常");
          }

      /*
         * 模糊查询数据*/
         @Override
         public List<ProdectDto> selectMhcx (String pname, Integer typeid) {
             List<ProdectDto> iProducts = dao.selectMhcx(pname,typeid);
             return iProducts;

            }

      /*
          * 根据ID查询数据*/
          @Override
          public R selectById(IProduct iProduct) {
              IProduct y=  dao.selectById(iProduct);
              return R.ok(dao.selectById(iProduct));
          }

     /*
     * 批量删除数据*/
         @Override
         public R deleteByIds(IProduct iProduct,String ids) {
             if(iProduct!=null){
             String[] arr = ids.split(",");
             List<Integer> list = new ArrayList<>();
             for (int i = 0; i < arr.length; i++) {
                    list.add(Integer.parseInt(arr[i]));
                }

                 if(dao.deleteByIds(list)>0){
                         return R.ok();
             }
         }
                  return R.fail("批量删除异常");
     }

}